package com.jservice.web.smkNews;

import com.jfinal.aop.Clear;
import com.jservice.framework.annotation.RouteBind;
import com.jservice.framework.authc.AuthcInterceptor;
import com.jservice.framework.dto.Message;
import com.jservice.framework.dto.Pager;
import com.jservice.model.SmkNews;
import com.jservice.web.base.JBaseController;

/**
 * NewsController - 新闻
 *
 * @author cwvian
 * @version 1.0
 * @date 2017/5/27
 */
@RouteBind(path = "/smk/news")
public class SmkNewsController extends JBaseController {

	public void list(){
		Pager pager = SmkNewsService.me().findPage(getPageable());

		setAttr("pager",pager);
		render("list");
	}

	public void add(){
		render("add");
	}

	public void save(){

		SmkNews news = getModel(SmkNews.class,"news");

		if(SmkNewsService.me().save(news)){
			setFlashMessage(Message.success("新闻添加成功"));
		}else{
			setFlashMessage(Message.error("新闻添加失败"));
		}
		redirect("/smk/news/list");
	}

	public void edit(){
		String newsId = getPara("id");
		SmkNews news = SmkNewsService.DAO.findById(newsId);
		setAttr("news",news);
		render("edit");
	}

	public void update(){

		SmkNews news = getModel(SmkNews.class,"news");
		if(SmkNewsService.me().update(news)){
			setFlashMessage(Message.success("新闻修改成功"));
		}else{
			setFlashMessage(Message.error("新闻修改失败"));
		}
		redirect("/smk/news/list");
	}

	public void delete(){
		String[] ids = getParaValues("ids");

		if (ids != null) {
			for (String id : ids) {
				SmkNews news = SmkNewsService.DAO.findById(id);
				if (news != null &&  SmkNews.status.ONLINE.name().equals(news.getStatus()) ) {
					renderJson(Message.error("不允许删除"));
					return;
				}
			}
			for(String id : ids){
				SmkNewsService.DAO.deleteById(id);
			}
		}
		renderJson(Message.success("删除成功"));
	}

	@Clear(AuthcInterceptor.class)
	public void preview(){
		String newsId = getPara("id");
		SmkNews news = SmkNewsService.DAO.findById(newsId);
		setAttr("news",news);
		render("preview");
	}

}
